<?php
class M_issues extends MY_Model {

	protected $_table = 'issues';
	
	public function __construct()
	{
		parent::__construct();
	}

	/**
	 * Get All Issues by Profile ID
	 * @param  int $profile_id
	 * @param int $resolved 1 or 0
	 * @return object
	 */
	public function get_all_issues($p_id, $resolved = false)
	{
		$filter = $resolved !== false ? " AND i.resolved = ? " : "";
		$sql = "
			SELECT
				i.*,
				sy.sy_from,
				sy.sy_to,
				l.level_desc,
				concat(em.last_name,', ',em.first_name,em.middle_name) as resolved_by
			FROM $this->_table i
			LEFT JOIN enrollments e ON i.e_id = e.e_id
			LEFT JOIN school_years sy ON e.e_ay_id = sy.id
			LEFT JOIN levels l ON e.e_grade_level = l.level_id
			LEFT JOIN users u ON i.resolved_by = u.id
			LEFT JOIN employees em ON u.id = em.id
			WHERE i.profile_id = ?
			$filter
			ORDER BY i.created_at DESC
		";

		return $this->query($sql, [$p_id, $resolved]);
	}

	/**
	 * Get Student issues - separate resolved and unresolved
	 * @param int $profile_id
	 */
	public function get_issues($profile_id)
	{	
		unset($r);
		$r['resolved'] = $this->get_all_issues($profile_id, 1);
		$r['unresolved'] = $this->get_all_issues($profile_id, 0);
		return (object)$r;
	}
}	
